package cn.cloudplug.core.index;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Interceptor;

/**
 * Created by pc on 2018/1/18.
 */

public class Configurator {

    private static class Holder{
        private static Configurator INSTANCE = new Configurator();
    }

    static Configurator getInstance(){return Holder.INSTANCE;}

    private static final HashMap<Object, Object> CONFIGS = new HashMap<>();
    private static final HashMap<Object, Object> MAP = new HashMap<>();

    private static boolean ready=false;

    private static Handler mainHandler = new Handler(Looper.getMainLooper());

    private Configurator(){}

    Configurator withGlideBuilder(GlideBuilder builder){
        checkNoReady();
        CONFIGS.put(ConfigKeys.GlideBuilder,builder);
        return this;
    }

    Configurator withAppContext(Context context){
        checkNoReady();
        CONFIGS.put(ConfigKeys.AppContext,context);
        return this;
    }

    public Configurator withBaseUrl(String url){
        checkNoReady();
        CONFIGS.put(ConfigKeys.BaseUrl,url);
        return this;
    }

    public Configurator withInterceptor(Interceptor inte){
        checkNoReady();
        List<Interceptor> interceptors = (List<Interceptor>) CONFIGS.get(ConfigKeys.Interceptor);
        if(interceptors==null){
            interceptors=new ArrayList<>();
            CONFIGS.put(ConfigKeys.Interceptor,interceptors);
        }
        interceptors.add(inte);
        return this;
    }

    public <T> T getConfiguration(Object object){
        checkReady();
        return (T) CONFIGS.get(object);
    }

    private void checkReady() {
        if(!ready){
            throw new RuntimeException("Configurator no exec configure()");
        }
    }

    private void checkNoReady() {
        if(ready){
            throw new RuntimeException("Configurator 已经调用了 configure()");
        }
    }

    public Handler getMainHandler(){
        return mainHandler;
    }

    public HashMap<Object, Object> getConfigs(){
        return CONFIGS;
    }

    public final void configure() {
        ready = true;

        int[] screenSize = new int[2];
        screenSize(screenSize);
        getConfigs().put(ConfigKeys.ScreenSize,screenSize);

        GlideBuilder builder = getConfiguration(ConfigKeys.GlideBuilder);
        if(builder!=null)
            Glide.init((Context)getConfiguration(ConfigKeys.AppContext), builder );
    }
    public void put(Object k,Object v){
        MAP.put(k, v);
    }

    public <T> T get(Object k) {
        return (T) MAP.get(k);
    }

    @SuppressLint("Assert")
    private void screenSize(int[] size){
        assert (size!=null&&size.length>=2);
        WindowManager manager  = (WindowManager) ((Context)getConfiguration(ConfigKeys.AppContext))
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        size[0] = outMetrics.widthPixels;
        size[1] = outMetrics.heightPixels;
    }
}
